package app.netant.book.wanshu;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.db.sql.DBManager;
import cn.lily.com.db.sql.manager.DBOfflineManager;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class WanshuNetRoot {
	
	private static String pageUrl = "http://www.wanshu5.net/lishi/";
	
	public static void analyse(int maxIdx){
		
		resume();
		
		System.out.println("整体处理开始........");
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = pageUrl + i+"/";
			
			System.out.println("第"+i+"页数据开始处理！");
			
			WanshuNetPage page = null;
			
			try {
				page = new WanshuNetPage(url);
				
				page.analyse();
				
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.NET_WANSHU_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");	
				continue;
			} finally {
				if(page != null){
					page.free();
				}
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static void analyse(int minIdx,int maxIdx){
		
		System.out.println("整体处理开始........");
		
		for(int i=minIdx;i<=maxIdx;i++){
			
			String url = pageUrl + i+"/";
			
			System.out.println("第"+i+"页数据开始处理！");
			
			WanshuNetPage page = null;
			
			try {
				page = new WanshuNetPage(url);
				
				page.analyse();
				
			} catch (Exception e) {
				e.printStackTrace();
				getDBManager().executeUpdate("insert into ${schema}.NET_WANSHU_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
						+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");	
				continue;
			}  finally {
				if(page != null){
					page.free();
				}
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static void excep(){
		
		System.out.println("异常处理开始........");
		
		getDBManager().executeUpdate("delete from ${schema}.NET_WANSHU_INFO "
				+ "where NOVEL_URL in "
				+ "(select EXCEP_URL from ${schema}.NET_WANSHU_EXCEP)");
		
		DataTable exptable = getDBManager().executeQueryTable("select * from ${schema}.NET_WANSHU_EXCEP");
		
		while(exptable.next()){
			
			Enviroment.getEnv().getLogBean().beginLog();
			
			try {
				
				String level = exptable.getString("EXCEP_LEVEL");
				String href = exptable.getString("EXCEP_URL");
				
				System.out.println("异常地址开始处理："+href);
				
				if("1".equals(level)){
					
					WanshuNetPage page = new WanshuNetPage(href);
					
					page.analyse();
					
					page.free();
					
				}else if("2".equals(level)){
					
					DataTable dataTable = getDBManager().executeQueryTable("select * from ${schema}.NET_WANSHU_INFO where NOVEL_URL='"+href+"'");
					if(dataTable.next()){
						continue;
					}
					
					WanshuEntity entity = new WanshuEntity();
					entity.setNovelUrl(href);
					
					WanshuNetDetail detail = new WanshuNetDetail(href);		
					detail.setEntity(entity);
					
					detail.analyse();
					
					detail.free();
					
				}
				
				getDBManager().executeUpdate("delete from ${schema}.NET_WANSHU_EXCEP where EXCEP_ID='"+exptable.getString("EXCEP_ID")+"'");
				
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
			
			Enviroment.getEnv().getLogBean().endLog();
			
		}
		
		System.out.println("异常处理结束........");
	
	}
	
	public static DBManager getDBManager(){
		return DBOfflineManager.getDBManager();
	}
	
	/**
	 * 循环异常处理
	 */
	public static void excepCycle(){
		
		System.out.println("循环异常处理开始...........");
		
		int i=0;
		while(true){
			
			System.out.println("第"+i+"次循环处理开始...........");
			
			excep();
			
			System.out.println("第"+i+"次循环处理结束...........");
			i++;
			
			DataTable excepTable = DBFactory.getThreadDB().executeQueryTable("select EXCEP_URL from ${schema}.NET_WANSHU_EXCEP");
			if(excepTable.getRecordCount() == 0){
				break;
			}
			
		}
		
	}
	
	/**
	 * 断点续传
	 */
	public static void resume(){
		
		System.out.println("断点续传处理开始........");
		
		getDBManager().executeUpdate("delete from ${schema}.NET_WANSHU_INFO "
				+ "where NOVEL_URL in "
				+ "(select EXCEP_URL from ${schema}.NET_WANSHU_EXCEP)");
		
		getDBManager().executeUpdate("delete from ${schema}.NET_WANSHU_EXCEP");
		
		System.out.println("断点续传处理结束........");
		
	}
	
	public static void main(String[] args) {
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		WanshuNetRoot.analyse(169);
		//WanshuNetRoot.excepCycle();

	}
	
}
